package com.longz.moonpie.demo;

import com.longz.moonpie.ApiClient;
import com.longz.moonpie.ApiClientImpl;
import com.longz.moonpie.domian.CreateOrderVo;
import com.longz.moonpie.domian.Result;
import com.longz.moonpie.enums.AccountType;
import com.longz.moonpie.request.CreateOrderRequest;
import com.longz.moonpie.request.CreateWithdrawRequest;
import com.longz.moonpie.request.OrderQueryRequest;
import com.longz.moonpie.util.ApiException;
import com.longz.moonpie.util.JsonUtil;

/**
 * @author: sichen.yuan
 * @date: 2021/3/8 14:41
 */
public class Demo {

    /**
     * 注意 Spring 项目可以用xml注入方式注入 ApiClientImpl
     * spring boot可以用@Configuration注入  ApiClientImpl
     *
     */
    private ApiClient apiClient = new ApiClientImpl("http://47.108.69.32:8866/api","UmmB5F","kIbzNhhcd1ROHusIYH49MloKaBSBNkXc");

    public static void main(String[] args) {
        Demo demo = new Demo();
        //获取UPI
//        demo.demoUpi();
        //创建代收订单
//        demo.demoCreateOrder();
        //查询代收订单状态
        demo.demoOrderQuery();
        //创建代付订单
//        demo.demoWithdraw();
        //查询代付订单-by tradeNo
//        demo.demoWithdrawQuery();
        //查询代付订单-by withdrawId
//        demo.demoWithdrawQueryById();
    }

    /**
     *  调用获取收款 UPI
     */
    public void demoUpi(){
        Result<String> upi = null;
        try {
            upi = apiClient.getUpi();
            System.out.printf("---->"+ JsonUtil.objectToJson(upi));
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用 创建代收订单
     */
    public void demoCreateOrder(){
        CreateOrderRequest request = new CreateOrderRequest();
        request.setTradeNo("uudsafasd9");
        request.setRefNo("sdaq24129");
        try {
            Result<CreateOrderVo> order = apiClient.createOrder(request);
            System.out.println("---->"+ JsonUtil.objectToJson(order));
            CreateOrderVo data = order.getData();
            System.out.printf("data ==>" + data.toString());
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用 查询代收订单状态
     */
    public void demoOrderQuery(){

        OrderQueryRequest request = new OrderQueryRequest();
//        request.setOrderNo("test_000001824489258");
//        request.setRefNo("sdaq24123");
        request.setTradeNo("uudsafasd9");
        try {
            Result<CreateOrderVo> query = apiClient.orderQuery(request);
            System.out.printf("data ==>" + query.getData());
            System.out.println("---->"+ JsonUtil.objectToJson(query));
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用 创建代付订单
     */
    public void demoWithdraw(){

        CreateWithdrawRequest request = new CreateWithdrawRequest();
        request.setAmount(1000);
        request.setBeneficaryName("Manish Bhardwaj");
        request.setPhoneNumber("Manish Bhardwaj");
        request.setAccountType(AccountType.vpa);
        request.setBeneficaryAccount("32254581974");
        request.setIfsCode("");
        request.setTradeNo("2020122709590965241312");
        request.setVpaAddress("");
        request.setNotifyUrl("");
        request.setEmail("");

        try {
            Result<Long> withdraw = apiClient.withdraw(request);
            System.out.println("---->"+ JsonUtil.objectToJson(withdraw));
        } catch (ApiException e) {
            e.printStackTrace();
        }

    }

    /**
     * 调用 查询代付订单-by tradeNo
     */
    public void demoWithdrawQuery(){
        try {
            //
            Result<Integer> integerResult = apiClient.withdrawQuery("2020122709590965241312");
            System.out.println("---->"+ JsonUtil.objectToJson(integerResult));
        } catch (ApiException e) {
            e.printStackTrace();
        }

    }

    /**
     * 调用 查询代付订单-by withdrawId
     */
    public void demoWithdrawQueryById(){

        try {
            Result<Integer> integerResult = apiClient.withdrawQuery(86906L);
            System.out.println("---->"+ JsonUtil.objectToJson(integerResult));
        } catch (ApiException e) {
            e.printStackTrace();
        }

    }


}
